﻿namespace MicroCloud.Options
{
    /// <summary>
    /// 系统配置选项
    /// </summary>
    public class MicroCloudOptions
    {
        /// <summary>
        /// 初始化一个系统配置选项 <see cref="MicroCloudOptions"/> 的新实例
        /// </summary>
        public MicroCloudOptions()
        {
            DbContexts = new ConcurrentDictionary<string, DbContextOption>(StringComparer.OrdinalIgnoreCase);
            OAuth2 = new ConcurrentDictionary<string, OAuth2Option>();
        }

        #region 属性
        /// <summary>
        /// 获取 数据上下文配置选项集合
        /// </summary>
        public IDictionary<string, DbContextOption> DbContexts { get; }

        /// <summary>
        /// 获取或设置 雪花算法配置选项
        /// </summary>
        public SnowflakeOption Snowflake { get; set; }

        /// <summary>
        /// 获取 第三方OAuth2登录配置选项
        /// </summary>
        public IDictionary<string, OAuth2Option> OAuth2 { get; }

        /// <summary>
        /// 获取或设置 Http传输加密选项
        /// </summary>
        public HttpEncryptOption HttpEncrypt { get; set; }

        /// <summary>
        /// 获取或设置 电子邮件配置
        /// </summary>
        public EmailOption Email { get; set; }

        /// <summary>
        /// 获取或设置 JWT身份认证选项
        /// </summary>
        public JwtOption Jwt { get; set; }

        /// <summary>
        /// 获取或设置 Cookie身份认证选项
        /// </summary>
        public CookieOption Cookie { get; set; }

        /// <summary>
        /// 获取或设置 Cors跨域策略
        /// </summary>
        public CorsOption Cors { get; set; }

        /// <summary>
        /// 获取或设置 Mvc选项
        /// </summary>
        public MvcOption Mvc { get; set; }

        /// <summary>
        /// 获取或设置 SignalR实时通信配置
        /// </summary>
        public SignalROption SignalR { get; set; }

        /// <summary>
        /// 获取或设置 缓存选项
        /// </summary>
        public RedisOption Redis { get; set; }

        #region "获取或设置 Swagger选项"
        /// <summary>
        /// 获取或设置 Swagger选项
        /// </summary>
        public SwaggerOption Swagger { get; set; }
        #endregion

        #endregion

        #region 方法
        /// <summary>
        /// 获取指定数据上下文类型的上下文配置信息
        /// </summary>
        /// <param name="dbContextType">数据上下文类型</param>
        /// <returns>数据上下文配置信息</returns>
        public DbContextOption GetDbContextOptions(Type dbContextType)
        {
            return DbContexts?.Values?.SingleOrDefault(m => m.DbContextType == dbContextType);
        }
        
        /// <summary>
        /// 获取指定数据上下文的上下文配置信息
        /// </summary>
        /// <param name="dbContext">数据上下文</param>
        /// <returns>数据上下文配置信息</returns>
        public DbContextOption GetDbContextOptions(IDbContext dbContext)
        {
            return GetDbContextOptions(dbContext.GetType());
        }

        #endregion

    }

}
